package com.citcc.web.controller.business;

import com.citcc.business.bean.*;
import com.citcc.business.bean.dto.CdzAmountDetailCon;
import com.citcc.business.service.CdzAmountDetailService;
import com.citcc.business.service.CdzAppUserService;
import com.citcc.business.service.CdzBalanceLService;
import com.citcc.business.service.UserOrderEntityService;
import com.citcc.common.annotation.Log;
import com.citcc.common.base.AjaxResult;
import com.citcc.common.enums.BusinessType;
import com.citcc.common.page.TableDataInfo;
import com.citcc.common.utils.DateUtils;
import com.citcc.common.utils.poi.ExcelUtil;
import com.citcc.framework.util.ShiroUtils;
import com.citcc.framework.web.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/appUser")
public class CdzAppUserController extends BaseController {

    private String prefix = "business/appUser";

    @Autowired
    private CdzAppUserService cdzAppUserService;
    @Autowired
    private UserOrderEntityService userOrderEntityService;
    @Autowired
    private CdzAmountDetailService cdzAmountDetailService;
    @Autowired
    private CdzBalanceLService cdzBalanceLService;

    @RequiresPermissions("business:appUser:view")
    @GetMapping("/main")
    public String vehicle(){
        return prefix + "/main";
    }

    @RequiresPermissions("business:appUser:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(CdzAppUser appUser){
        startPage();
        List<CdzAppUser> list=cdzAppUserService.selectAllList(appUser);
        return getDataTable(list);
    }

    @RequiresPermissions("business:appUser:export")
    @Log(title = "客户导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CdzAppUser appUser)
    {
        List<CdzAppUser> list=cdzAppUserService.selectAllList(appUser);
        ExcelUtil<CdzAppUser> util = new ExcelUtil<CdzAppUser>(CdzAppUser.class);
        return util.exportExcel(list, DateUtils.dateTimeNow()+"客户信息");
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id")String id, ModelMap map)
    {
        map.put("appUser",cdzAppUserService.selectByPrimaryKey(id));
        return prefix + "/edit";
    }

    @RequiresPermissions("business:appUser:edit")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/editSave")
    @ResponseBody
    public AjaxResult editSave(CdzAppUser appUser)
    {
        appUser.setUpdateTime(new Date());
        return toAjax(cdzAppUserService.update(appUser));
    }

    /**
     * 充电记录
     */
    @RequiresPermissions("business:charge:view")
    @GetMapping("/view")
    public String charge(ModelMap map){
        map.put("count",userOrderEntityService.selectSumCharge());
        return prefix + "/charge";
    }

    @RequiresPermissions("business:charge:list")
    @GetMapping("/chargeList")
    @ResponseBody
    public TableDataInfo chargeList(CdzChargeInfo chargeInfo){
        startPage();
        List<CdzChargeInfo> list=userOrderEntityService.getAllChargeInfo(chargeInfo);
        return getDataTable(list);
    }

    //删除充电记录
    @RequiresPermissions("business:charge:remove")
    @Log(title = "充值管理管理", businessType = BusinessType.UPDATE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String id)
    {
        try
        {
            int count=cdzAmountDetailService.delete(id);
            return toAjax(count);
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
}
